04 / 17

Why is the worst-case lookup O(n)? How do modern implementations avoid this? (Tree buckets)

Worst-Case Lookup and Tree Buckets

javascript
  1. 1

    Good hash distribution normally keeps bucket sizes small.

  2. 2

    Treeification protects against pathological collision chains in some implementations.

  3. 3

    Tree-based buckets can provide O(log k) lookup within a bucket.

  4. 4

    Treeification has additional memory and comparison overhead.

  5. 5

    The exact thresholds and implementation strategy are library-specific.

  6. 6

    This optimization does not mean all Hash Table operations are unconditionally O(log n); expected behavior remains approximately O(1).

Difficulty: 4/10

Follow-up Questions

  • How does Java HashMap treeification work?
  • Why not use trees for every bucket?
  • What are the memory trade-offs?